﻿using System;

namespace DistributedVisionRunner.Module.Helper
{
    public class NamingSectionParser
    {
        private readonly char _start;
        private readonly char _end;

        public NamingSectionParser((char start, char end) sectionDelimiter)
        {
            _start = sectionDelimiter.start;
            _end = sectionDelimiter.end;
        }

        public string Parse(string text)
        {
            var startIndex = text.IndexOf(_start);
            if (startIndex == -1) return null;
            try
            {
                var endIndex = Equals(_start, _end) ? text.IndexOf(_end, startIndex + 1) : text.IndexOf(_end);
                if (endIndex == -1) return null;

                var output = text.Substring(startIndex+1, endIndex - startIndex - 1);
                return string.IsNullOrWhiteSpace(output) ? null : output;
            }
            catch (ArgumentOutOfRangeException)
            {
                return null;
            }


        }
    }
}